﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Odbc;
using Org.SqlHelper;

namespace QualityRpt.Dao.Db2
{
    class CodeConfigDao:ICodeConfigDao
    {

        public DataRow Save(DataRow row)
        {
            object rt = null;
            using (OdbcConnection conn = ODBCHelper.GetConnection())
            {
                ODBCHelper.ExecuteNonQuery(conn, CommandType.Text, "INSERT INTO RPT.CODE_CONFIG(DEVICE_ID,TARGET,CONTENT,REMARK) VALUES(?,?,?,?)",
                    ODBCHelper.CreateInputParameter("DEVICE_ID", OdbcType.Int, Convert.ToInt32(row["DEVICE_ID"])),
                    ODBCHelper.CreateInputParameter("TARGET", OdbcType.Int, Convert.ToInt32(row["TARGET"])),
                    ODBCHelper.CreateInputParameter("CONTENT", OdbcType.VarChar,255, row["CONTENT"].ToString()),
                    ODBCHelper.CreateInputParameter("REMARK", OdbcType.VarChar, 200, row["REMARK"].ToString()));
                rt = ODBCHelper.ExecuteScalar(conn, CommandType.Text, "SELECT IDENTITY_VAL_LOCAL() FROM SYSIBM.SYSDUMMY1");
            }
            if (rt != DBNull.Value)
            {
                row["ID"] = Convert.ToInt32(rt);
                return row;
            }
            return null;
        }

        public DataRow Update(DataRow row)
        {
            using (OdbcConnection conn = ODBCHelper.GetConnection())
            {
                ODBCHelper.ExecuteNonQuery(conn, CommandType.Text, "UPDATE RPT.CODE_CONFIG SET DEVICE_ID=?,TARGET=?,CONTENT=?,REMARK=? WHERE ID=?",
                    ODBCHelper.CreateInputParameter("DEVICE_ID", OdbcType.Int, Convert.ToInt32(row["SUPPLIER_NAME"])),
                    ODBCHelper.CreateInputParameter("TARGET", OdbcType.Int, Convert.ToInt32(row["TARGET"])),
                    ODBCHelper.CreateInputParameter("CONTENT", OdbcType.VarChar, 255, row["CONTENT"].ToString()),
                    ODBCHelper.CreateInputParameter("REMARK", OdbcType.VarChar, 200, row["REMARK"].ToString()),
                    ODBCHelper.CreateInputParameter("ID", OdbcType.VarChar, 200, row["ID"].ToString()));
            }
            DataTable rtTable = GetList("ID=" + row["ID"].ToString());
            if (rtTable.Rows.Count > 0)
            {
                return rtTable.Rows[0];
            }
            return null;
        }

        public DataRow Get(long id)
        {
            DataTable rtTable = GetList("ID=" + id.ToString());
            if (rtTable.Rows.Count > 0)
            {
                return rtTable.Rows[0];
            }
            return null;
        }

        public void Delete(long id)
        {
            using (OdbcConnection conn = ODBCHelper.GetConnection())
            {
                ODBCHelper.ExecuteNonQuery(conn, CommandType.Text, "DELETE FROM RPT.CODE_CONFIG WHERE ID=?",
                    ODBCHelper.CreateInputParameter("ID", OdbcType.Int, id));
            }
        }

        public DataTable GetList(string where)
        {
            return GetList(where, "ID ASC");
        }

        public DataTable GetList(string where, string sort)
        {
            StringBuilder sbText = new StringBuilder();
            sbText.Append("SELECT * FROM RPT.CODE_CONFIG ");
            if (where.Trim() != string.Empty)
            {
                sbText.Append(" WHERE "+where);
            }
            if (sort.Trim() != string.Empty)
            {
                sbText.Append(" ORDER BY " + sort);
            }
            using (OdbcConnection conn = ODBCHelper.GetConnection())
            {
                return ODBCHelper.ReadTable(conn, CommandType.Text,sbText.ToString());
            }
        }

        public DataTable GetPageList(Utils.PageUtils pu, string where, string sort)
        {
            throw new NotImplementedException();
        }
    }
}
